﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		DexterControllerFactory.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Dexter.Dependency;
using Dexter.Logger.Contracts;

namespace Dexter.Web.Mvc.Controllers {
	/// <summary>
	/// 	Defines a controller provider for all mvc controllers.
	/// </summary>
	public class DexterControllerFactory : DefaultControllerFactory {
		readonly IDexterContainer container;
		readonly ILogger logger;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "DexterControllerFactory" /> class.
		/// </summary>
		/// <param name = "container">The container.</param>
		/// <param name = "logger">The logger.</param>
		public DexterControllerFactory ( IDexterContainer container , ILogger logger ) {
			this.container = container;
			this.logger = logger;
		}

		/// <summary>
		/// 	Creates the specified controller by using the specified request context.
		/// </summary>
		/// <param name = "requestContext">The context of the HTTP request, which includes the HTTP context and route data.</param>
		/// <param name = "controllerName">The name of the controller.</param>
		/// <returns>A reference to the controller.</returns>
		/// <exception cref = "T:System.ArgumentNullException">The <paramref name = "requestContext" /> parameter is null.</exception>
		/// <exception cref = "T:System.ArgumentException">The <paramref name = "controllerName" /> parameter is null or empty.</exception>
		public override IController CreateController ( RequestContext requestContext , string controllerName ) {
			Type controllerType = GetControllerType ( requestContext , controllerName );

			if ( controllerType == null ) {
				string message = string.Format ( "Could not find a controller for the request for {0}." , controllerName );
				logger.Debug ( message );
				throw new HttpException ( 404 , message );
			}

			return GetControllerInstance ( requestContext , controllerType );
		}

		/// <summary>
		/// 	Retrieves the controller instance for the specified request context and controller type.
		/// </summary>
		/// <param name = "requestContext">The context of the HTTP request, which includes the HTTP context and route data.</param>
		/// <param name = "controllerType">The type of the controller.</param>
		/// <returns>The controller instance.</returns>
		/// <exception cref = "T:System.Web.HttpException">
		/// 	<paramref name = "controllerType" /> is null.</exception>
		/// <exception cref = "T:System.ArgumentException">
		/// 	<paramref name = "controllerType" /> cannot be assigned.</exception>
		/// <exception cref = "T:System.InvalidOperationException">An instance of <paramref name = "controllerType" /> cannot be created.</exception>
		protected override IController GetControllerInstance ( RequestContext requestContext , Type controllerType ) {
			if ( controllerType != null ) {
				logger.DebugFormat ( "GetControllerInstance for {0}" , controllerType.Name );
				return ( IController ) container.Resolve ( controllerType );
			}

			logger.Debug ( "GetControllerInstance for null controller type" );
			return null;
		}
	}
}
